package com.bjpowernode.crm.workbench.web.controller;

import com.bjpowernode.crm.commons.contants.Contants;
import com.bjpowernode.crm.commons.domain.ReturnObject;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.settings.service.DicValueService;
import com.bjpowernode.crm.settings.service.UserService;
import com.bjpowernode.crm.workbench.domain.Activity;
import com.bjpowernode.crm.workbench.domain.Clue;
import com.bjpowernode.crm.workbench.domain.ClueActivityRelation;
import com.bjpowernode.crm.workbench.dto.ClueConvertingDto;
import com.bjpowernode.crm.workbench.service.*;
import lombok.Getter;
import lombok.Setter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * 处理线索相关请求
 * 发现课堂源码中没有设计分页查询线索的接口，刘做作业，页面中的是静态数据
 * 因此只要接口中需要线索 id 作为参数，都需要手动输入地址进行跳转
 * 按照设定这应该是一个 Ajax 请求
 * 我不能写前端，也不实现了
 * 但每次都要手动携带线索 id 请求对应页面，实在太麻烦，我还是在跳转到线索主页的时候把所有线索携带过去吧
 *
 * @author 刘逸晖
 */
@RequestMapping
@Getter
@Setter
public class ClueController {

    /**
     * 处理线索相关业务
     */
    private ClueService clueService;

    /**
     * 处理用户相关业务
     */
    private UserService userService;

    /**
     * 处理字典值相关业务
     */
    private DicValueService dicValueService;

    /**
     * 处理市场活动相关业务
     */
    private ActivityService activityService;

    /**
     * 处理线索备注相关业务
     */
    private ClueRemarkService clueRemarkService;

    /**
     * 处理线索与市场活动关联相关业务
     */
    private ClueActivityRelationService clueActivityRelationService;

    /**
     * 跳转到线索主页面
     *
     * @return 模型和试图
     */
    @GetMapping("/workbench/clue/index.do")
    public ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("userList", this.userService.queryAllUsers())
                .addObject("appellationList", this.dicValueService.queryDicValueByTypeCode("appellation"))
                .addObject("clueStateList", this.dicValueService.queryDicValueByTypeCode("clueState"))
                .addObject("sourceList", this.dicValueService.queryDicValueByTypeCode("source"))
                .addObject("clueList", this.clueService.queryCluesByConditions(null))//新加的
                .setViewName("workbench/clue/index");
        return modelAndView;
    }

    /**
     * 创建线索
     *
     * @param clue    线索信息
     * @param session http 会话
     * @return 创建结果
     */
    @PostMapping("/workbench/clue/saveCreateClue.do")
    @ResponseBody
    public ReturnObject saveCreateClue(Clue clue, HttpSession session) {
        this.clueService.insertClue(clue, session);

        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        returnObject.setMessage("恭喜，创建线索成功");
        return returnObject;
    }

    /**
     * 查询线索详情
     *
     * @param id 线索 id
     * @return 线索详情页
     */
    @GetMapping("/workbench/clue/detailClue.do")
    public ModelAndView detailClue(String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("clue", this.clueService.queryClueById(id))
                .addObject("remarkList", this.clueRemarkService.queryClueRemarkByClueId(id))
                .addObject("activityList", this.activityService.queryActivityByClueId(id))
                .setViewName("workbench/clue/detail");

        return modelAndView;
    }

    /**
     * 查询还没有与某线索关联的市场活动，可根据市场活动的名称进行过滤
     *
     * @param activityName 市场活动的名称，可为空
     * @param clueId       线索 id
     * @return 市场活动列表
     */
    @PostMapping("/workbench/clue/queryActivityForDetailByNameClueId.do")
    @ResponseBody
    public List<Activity> queryActivityForDetailByNameClueId(String clueId, String activityName) {
        return this.clueActivityRelationService.queryActivitiesNotRelated(clueId, activityName);
    }

    /**
     * 线索关联市场活动
     *
     * @param activityId 预关联市场活动的 id 列表
     * @param clueId     线索 id
     * @return 操作结果
     */
    @PostMapping("/workbench/clue/saveBund.do")
    @ResponseBody
    public ReturnObject saveBund(String[] activityId, String clueId) {
        this.clueActivityRelationService.relateActivitiesByIds(activityId, clueId);

        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        returnObject.setMessage("恭喜，线索关联市场活动成功");
        returnObject.setRetData(this.activityService.queryActivitiesByIds(activityId));
        return returnObject;
    }

    /**
     * 解除线索和市场活动的关联关系
     *
     * @param clueActivityRelation 线索和市场活动的 id
     * @return 操作结果
     */
    @PostMapping("/workbench/clue/saveUnbund.do")
    @ResponseBody
    public ReturnObject saveUnbund(ClueActivityRelation clueActivityRelation) {
        this.clueActivityRelationService.deleteClueActivityRelationByClueIdAndActivityId(clueActivityRelation.getClueId(), clueActivityRelation.getActivityId());

        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        returnObject.setMessage("恭喜，解除关联线索与市场活动成功");
        return returnObject;
    }

    /**
     * 跳转到线索转换页面
     *
     * @param id 线索 id
     * @return 模型和试图
     */
    @GetMapping("/workbench/clue/toConvert.do")
    public ModelAndView toConvert(String id) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("clue", this.clueService.queryClueById(id))
                .addObject("stageList", this.dicValueService.queryDicValueByTypeCode("stage"))
                .setViewName("workbench/clue/convert");

        return modelAndView;
    }

    /**
     * 查询与指定线索关联的市场活动，可根据市场活动的名称模糊查询
     *
     * @param activityName 市场活动的名称
     * @param clueId       线索 id
     * @return 市场活动列表
     */
    @PostMapping("/workbench/clue/queryActivityForConvertByNameClueId.do")
    @ResponseBody
    public List<Activity> queryActivityForConvertByNameClueId(String activityName, String clueId) {
        return this.clueActivityRelationService.queryRelatedActivities(activityName, clueId);
    }

    /**
     * 转换线索到客户、联系人，创建交易
     *
     * @param convertingClueDto 相关参数
     * @param httpSession       http 会话
     * @return 转换结果
     */
    @PostMapping("/workbench/clue/convertClue.do")
    @ResponseBody
    public ReturnObject convertClue(ClueConvertingDto clueConvertingDto, HttpSession httpSession) {
        this.clueService.convertClue(clueConvertingDto, (User) httpSession.getAttribute(Contants.SESSION_USER));

        ReturnObject returnObject = new ReturnObject();
        returnObject.setCode(Contants.RETURN_OBJECT_CODE_SUCCESS);
        returnObject.setMessage("恭喜，转换线索成功");
        return returnObject;
    }

}
